# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

'''
drop database guazidb;
create database guazidb charset utf8;
use guazidb;
create table guazi_tab(
href varchar(500),
title varchar(100),
price varchar(100)
)charset=utf8;
'''
from .settings import *

# 管道1：终端打印输出汽车名称
class GuaziPipeline(object):
    def process_item(self, item, spider):

        print(item['title'])

        return item

# 管道2：持久化到mysql数据库
import pymysql
class GuaziMysqlPipeline(object):
    def open_spider(self, spider):
        """爬虫项目启动时,只执行一次,用于数据库连接"""
        self.db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PWD, database=MYSQL_DB, charset=MYSQL_CHARSET)
        self.cur = self.db.cursor()
        self.ins = 'insert into guazi_tab values(%s,%s,%s)'
        print('君不见黄河之水天上来,奔流到海不复回')

    def process_item(self, item, spider):
        li = [
            item['href'],
            item['title'],
            item['price'],
        ]
        self.cur.execute(self.ins, li)
        self.db.commit()

        return item

    def close_spider(self, spider):
        """爬虫项目结束时,只执行一次,用于断开数据库"""
        self.cur.close()
        self.db.close()
        print('十年生死两茫茫，不思量，自难忘')

# 管道3：持久化到mongdb数据库
import pymongo
class GuaziMongoPipeline(object):
    def open_spider(self, spider):
        """连接mongodb数据库"""
        self.conn = pymongo.MongoClient(host=MONGO_HOST, port=MONGO_PORT)
        self.db = self.conn[MONGO_DB]
        self.myset = self.db[MONGO_SET]

    def process_item(self, item, spider):

        self.myset.insert_one(dict(item))

        return item









